package cn.wuqingna.legendgem.gem

import cn.wuqingna.legendgem.loader.GemLoader
import cn.wuqingna.legendgem.random.RandomData
import cn.wuqingna.legendgem.util.convertMapToStringMap
import com.google.gson.Gson
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import taboolib.module.configuration.util.asMap
import taboolib.module.nms.getItemTag
import taboolib.platform.util.hasLore


object GemManager {
    val gson = Gson()
    fun getItemGems(item: ItemStack, player: Player): MutableList<ItemStack>{
        return if (item.getItemTag().getDeep("legendGem.GEMDATA") != null) {
            val list = mutableListOf<ItemStack>()
            val group = item.getItemGemGroup()
            group.forEach { gemGroup ->
                gemGroup.gemCaches.forEach {
                    list += GemPlant.buildGem(player, it)
                }
            }
            list
        } else {
            mutableListOf()
        }
    }



    fun getItemAttribute(item: ItemStack, player: Player): MutableList<String>{
        return if (item.getItemTag().getDeep("legendGem.gem-key") != null) {

            val cache = item.getCacheGem()
            val map = convertMapToStringMap(cache.map).toMutableMap()

            val random = RandomData(player, map)

            val gem = GemLoader.gems[cache.key]!!
            random.scriptStr(gem.attribute)


        } else {
            mutableListOf()
        }
    }



    fun getGem(key: String): Gem {
        return try {
            GemLoader.gems[key]!!
        } catch (e: Exception) {
            error("尝试获取一个不存在的宝石 $key")
        }
    }

    fun ItemStack.modifyLore(lore: String, newLore: String){
        val meta = this.itemMeta
        val metaLore = meta.lore
        val index = meta.lore.indexOf(lore)
        if (index != -1){
            metaLore[index] = newLore
            meta.lore = metaLore
            this.itemMeta = meta
        }
    }

    fun ItemStack.saveGemGroup(data: MutableList<GemGroup>){
        val tag = getItemTag()
        val list = mutableListOf<String>()
        data.forEach {
            list += gson.toJson(it)
        }
        tag.putDeep("legendGem.GEMDATA", list)
        tag.saveTo(this)
    }

    fun ItemStack.getCacheGem(): CacheGem{
        val tag = getItemTag()
        return CacheGem(tag.getDeep("legendGem.gem-key")!!.asString(),
            (tag.getDeep("legendGem.cache")!!.asMap())
        )
    }


    fun ItemStack.getItemGemGroup(): MutableList<GemGroup>{
        val groups = mutableListOf<GemGroup>()
        val holePosition = mutableListOf<String>()
        GemLoader.gems.values.forEach { holePosition += it.holePosition }
        val deep = this.getItemTag().getDeep("legendGem.GEMDATA")
        if (deep != null) {
            val list = deep.asList()
            list.forEach {data ->
                groups += gson.fromJson(data.asString(), GemGroup::class.java)
            }
        } else {
            if (this.hasLore()){
                val tag = getItemTag()
                this.itemMeta.lore.forEach {lore ->
                    //判断 能不能镶嵌
                    if (holePosition.any{ it in lore}) {
                        groups += GemGroup(lore, cacheLore = lore)
                    }
                }
                val data = mutableListOf<String>()
                groups.forEach {
                    data += gson.toJson(it)
                }
                tag.putDeep("legendGem.GEMDATA", data)
                tag.saveTo(this)
            }
        }


        return groups

    }

}